package com.guigu.lc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.mapper.*;
import com.guigu.common.pojo.DesignModule;
import com.guigu.common.pojo.MDesignProcedure;
import com.guigu.common.pojo.MDesignProcedureDetails;
import com.guigu.common.vo.SearchVo;
import com.guigu.lc.serach.changeproductionprocessSerach;
import com.guigu.lc.service.productionprocessesService;
import com.guigu.lc.view.mdesignprocedureview;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 产品生产工序设计单审核，产品生产工序设计单查询，产品生产工序设计单变更的productionprocessesService的实现类
 */
@Service
public class productionprocessesServiceimpl implements productionprocessesService {

    @Resource
    private MDesignProcedureMapper designProcedureMapper;

    @Resource
    private MDesignProcedureDetailsMapper detailsMapper;

    @Resource
    private DFileMapper dfileMapper;

    @Resource
    private DesignModuleMapper designModuleMapper;

    @Resource
    private MDesignProcedureModuleMapper mdDesignProcedureModuleMapper;

    @Resource
    private DModuleDetailsMapper moduleDetailsMapper;

    @Override
    public SearchVo<MDesignProcedure> queryBycheckService(SearchVo<MDesignProcedure> searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo page=new PageInfo(this.designProcedureMapper.queryBycheck(searchVo));
        SearchVo<MDesignProcedure> vo=new SearchVo<MDesignProcedure>();
        vo.setTotal(page.getTotal());
        vo.setRows(page.getList());
        return vo;
    }

    @Override
    public SearchVo<MDesignProcedure> queryproductionallService(SearchVo<MDesignProcedure> searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo page=new PageInfo(this.designProcedureMapper.queryproductionall(searchVo));
        SearchVo<MDesignProcedure> vo=new SearchVo<MDesignProcedure>();
        vo.setTotal(page.getTotal());
        vo.setRows(page.getList());
        return vo;
    }

    @Override
    public changeproductionprocessSerach querychangeproductionService(changeproductionprocessSerach serach) {
        PageHelper.startPage(serach.getPageNum(),serach.getPageSize());
        PageInfo page=new PageInfo(this.designProcedureMapper.querychangeproduction(serach));
        changeproductionprocessSerach changeserach=new changeproductionprocessSerach();
        changeserach.setTotal(page.getTotal());
        changeserach.setRows(page.getList());
        return changeserach;
    }

    @Override
    public List<MDesignProcedureDetails> queryByproductioncheckmingxiService(int id) {
        return this.detailsMapper.queryByproductioncheckmingxi(id);
    }

    @Override
    public void productshengheok(mdesignprocedureview mdesignprocedureview) {
        this.detailsMapper.updatedesignprocedurek(mdesignprocedureview);
        this.designProcedureMapper.updatedesignproductok(mdesignprocedureview);
    }

    @Override
    public void productshengheno(mdesignprocedureview mdesignprocedureview) {
        this.dfileMapper.updateByproductidortag(mdesignprocedureview.getMdesignProcedure().getProductId());
        this.designProcedureMapper.updatedesignproductno(mdesignprocedureview);
    }

    /**
     *
     * @param mdesignprocedureview
     * 1．	修改 design_module 的check_tag 为未审核 （S001-0）
     *
     */
    @Override
    public void changenoshejiService(mdesignprocedureview mdesignprocedureview) {
        DesignModule designModule = this.designModuleMapper.queryByproductid(mdesignprocedureview.getMdesignProcedure().getProductId());
        if (designModule!=null){
            System.out.println("审核通过就删除");
        //this.moduleDetailsMapper.deletefromparentyige(designModule.getId());
        this.designModuleMapper.updatedesignmodulechecktagfalse(mdesignprocedureview.getMdesignProcedure().getProductId());
        }
        this.designProcedureMapper.updatemdesignproductfalse(mdesignprocedureview);
        this.detailsMapper.deletefromparentid(mdesignprocedureview.getMdesignProcedure().getId());
        this.detailsMapper.insertchangeproceduredetails(mdesignprocedureview);

    }

    /**
     * 产品工序审核：未做工序物料设计 工序物料设计已设计
     * @param mdesignprocedureview
     */
    @Override
    public void changeokshejiService(mdesignprocedureview mdesignprocedureview) {
        DesignModule designModule = this.designModuleMapper.queryByproductid(mdesignprocedureview.getMdesignProcedure().getProductId());
        if(designModule!=null){
        //this.moduleDetailsMapper.deletefromparentyige(designModule.getId());
        this.designModuleMapper.updatedesignmodulechecktagfalse(mdesignprocedureview.getMdesignProcedure().getProductId());
        }
        this.designProcedureMapper.updatemdesignproductfalse(mdesignprocedureview);
        this.detailsMapper.deletefromparentid(mdesignprocedureview.getMdesignProcedure().getId());
        this.detailsMapper.insertchangeproceduredetails(mdesignprocedureview);
        this.mdDesignProcedureModuleMapper.deletefromchanpgxsdheji(mdesignprocedureview);
        this.designProcedureMapper.updatedesignmoduletagfalse(mdesignprocedureview.getMdesignProcedure().getDesignId());

    }
}
